package cn.com.surker.system.service.impl;

import cn.com.surker.enums.StateEnum;
import cn.com.surker.system.dao.SysPositionDao;
import cn.com.surker.system.entity.SysPosition;
import cn.com.surker.system.service.SysPositionService;
import cn.com.surker.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author luotao
 * @date 2022/11/18
 */
@Service
public class SysPositionServiceImpl extends ServiceImpl<SysPositionDao, SysPosition> implements SysPositionService {

	@Resource
	private SysPositionDao positionDao;

	@Override
	public Integer delete(Long id) {
		if(id == null){
			return -1;
		}
		SysPosition sysPosition = new SysPosition();
		sysPosition.setId(id);
		sysPosition.setStatus(StateEnum.DISABLED.getCode());
		int i = positionDao.updateById(sysPosition);
		return i;
	}

	@Override
	public List<SysPosition> findList(String name) {
		QueryWrapper<SysPosition> qw = new QueryWrapper<>();
		qw.eq("status",StateEnum.ENABLED.getCode());
		if(StringUtils.isNotBlank(name)){
			qw.like("name",name);
		}
		List<SysPosition> sysPositions = positionDao.selectList(qw);
		return sysPositions;
	}

	@Override
	public IPage<SysPosition> page(String name, Integer pageIndex, Integer limit) {
		PageHelper.startPage(pageIndex,limit);
		IPage<SysPosition> pa = new Page<>(pageIndex,limit);
		QueryWrapper<SysPosition> qw = new QueryWrapper<>();
		qw.eq("status",StateEnum.ENABLED.getCode());
		if(StringUtils.isNotBlank(name)){
			qw.like("name",name);
		}
		IPage<SysPosition> iPage = positionDao.selectPage(pa, qw);
		if (!StringUtils.isNull(iPage.getRecords())) {
			PageInfo pageInfo = new PageInfo(iPage.getRecords());
			if (!StringUtils.isNull(pageInfo.getTotal()))
				iPage.setTotal(pageInfo.getTotal());
		}
		return iPage;
	}

	@Override
	public Boolean isExist(String name) {
		QueryWrapper<SysPosition> qw = new QueryWrapper();
		qw.eq("status",StateEnum.ENABLED.getCode());
		if(StringUtils.isNotBlank(name)){
			qw.eq("name",name);
		}
		List<SysPosition> list = positionDao.selectList(qw);
		if(list != null && !list.isEmpty()){
			return true;
		}else {
			return false;
		}
	}

	@Override
	public boolean save(SysPosition entity) {
		if (entity == null){
			return false;
		}
		if(isExist(entity.getName())){
			return false;
		}
		entity.setStatus(StateEnum.ENABLED.getCode());
		entity.setCreateTime(new Date());
		return super.save(entity);
	}

	@Override
	public boolean updateById(SysPosition entity) {
		if(entity == null){
			return false;
		}
		if(isExist(entity.getName())){
			return false;
		}
		entity.setStatus(StateEnum.ENABLED.getCode());
		return super.updateById(entity);
	}
}
